/**
 * @file main.cpp
 * @author your name (you@domain.com)
 * @brief
 * @version 0.1
 * @date 2023-12-23
 *
 * @copyright Copyright (c) 2023
 * 程序入口
 */
#include "./im.h"
#include "./src/mysql/MysqlConnectionPool.h"
#include "./src/IMServer.hpp"

int main(int argc, char *argv[])
{
    ptree pt;
    read_ini("./conf/chat.ini", pt);

    std::string listenip = pt.get<std::string>("client.listenip");
    int listenport = pt.get<int>("client.listenport");

    std::string monitorlistenip = pt.get<std::string>("monitor.monitorlistenip");
    int monitorlistenport = pt.get<int>("monitor.monitorlistenport");
    int monitortoken = pt.get<int>("monitor.monitortoken");

    std::string httplistenip = pt.get<std::string>("http.httplistenip");
    int httplistenport = pt.get<int>("http.httplistenport");

    std::string logfiledir = pt.get<std::string>("log.logfiledir");
    std::string logfilename = pt.get<std::string>("log.logfilename");
    bool logbinarypackage = pt.get<bool>("log.logbinarypackage");

    std::string dbserver = pt.get<std::string>("mysql.dbserver");
    std::string dbuser = pt.get<std::string>("mysql.dbuser");
    std::string dbpwd = pt.get<std::string>("mysql.dbpassword");
    std::string dbname = pt.get<std::string>("mysql.dbname");
    size_t dbpoolsize = pt.get<size_t>("mysql.dbpoolsize");

    size_t threadpoolsize = pt.get<size_t>("threadpool.threadpoolsize");

    // 添加常见的日志属性，如时间戳和线程ID
    logging::add_common_attributes();

    // 设置日志格式
    logging::formatter fmt = expr::stream
                             << "[" << expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d %H:%M:%S.%f") // 修改这行
                             << "] [" << expr::attr<logging::attributes::current_thread_id::value_type>("ThreadID")
                             << "] [" << logging::trivial::severity
                             << "] " << expr::smessage;
    // 将格式应用到控制台日志
    logging::add_console_log(std::clog, boost::log::keywords::format = fmt);
    // 创建一个过滤器
    auto filter = expr::attr<logging::trivial::severity_level>("Severity") >= logging::trivial::info;

    // 将日志消息写入文件
    logging::add_file_log("im.log", boost::log::keywords::format = fmt,
                          boost::log::keywords::filter = filter);

    // Print values
    LOG_I << "Thread Pool Size: " << threadpoolsize << "\n";
    LOG_I << "Listen IP: " << listenip << "\n";
    LOG_I << "Listen Port: " << listenport << "\n";

    try
    {
        boost::asio::io_context io;
        // tcp::endpoint endpoint(tcp::v4(), listenport);

        // // 创建多个线程，每个线程都运行 io_context::run 函数
        // std::vector<std::thread> threads;
        // for (size_t i = 0; i < threadpoolsize; ++i)
        //     threads.emplace_back([&io]
        //                          { io.run(); });

        // // 等待所有线程完成
        // for (auto &t : threads)
        //     t.join();
        MysqlConnectionPool::initInstance(io, dbpoolsize, dbserver, dbuser, dbpwd, dbname);
        IMServer imserver(io, listenport);
        io.run();
    }
    catch (const std::exception &e)
    {
        std::cerr << "Exception: " << e.what() << "\n";
    }

    return 0;
}